package com.emm.yixun.website.controller.customer;
//框架包
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

//变化的引用
import com.emm.yixun.common.model.CustomerAnswer;
import com.emm.yixun.common.model.CustomerAnswerInfo;
import com.emm.yixun.common.model.Project;
import com.emm.yixun.common.model.ProjectQuestionnaire;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.CustomerAnswerDto;
import com.emm.yixun.common.model.dto.ProjectDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.page.Page;
//common
import com.emm.yixun.common.page.PageUtil;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.customer.service.CustomerAnswerServiceFacade;
import com.emm.yixun.customer.service.ProjectQuestionnaireServiceFacade;
import com.emm.yixun.estate.service.ProjectServiceFacade;
//website
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;




@Controller
@RequestMapping("/customer/customerAnswer/")
public class CustomerAnswerController extends BaseController<CustomerAnswer>{
	
	@Autowired
	private CustomerAnswerServiceFacade customerAnswerServiceFacade;
	
	@Autowired
	private ProjectServiceFacade projectServiceFacade;
	
	@Autowired
	private ProjectQuestionnaireServiceFacade projectQuestionnaireServiceFacade;
	
	@RequestMapping(value="index")
	public String index(ModelMap map){
		//if(doSecurityIntercept(Const.RESOURCES_TYPE_MENU)){
			//判断是否拥有项目列表【全部】权限
	        map.put("permitBtn", doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "customer:list:all")); 
			
			//查询项目
			User user=ShiroUtil.getCurrentUser();
			ProjectDto entity = new ProjectDto();
			entity.setMerchantId(user.getMerchantId());
			entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
			entity.setResourcesCode("menu_0003_0001");
			entity.setPermissionUserId(user.getId());
			ResponseList<Project> res = projectServiceFacade.findByDto(entity);
			List<Project> projectList = new ArrayList<Project>();
			if(res.isSuccess() && null != res.getEntity() && res.getEntity().size() > 0){
				projectList = res.getEntity();
			}
			map.addAttribute("projectList", projectList);
			
			return "/customer/questionnaire/historicalRecord";
		//}
		//return Const.NO_AUTHORIZED_URL;
	}
	
	//新增返回实体对象
	@RequestMapping(value="add", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  add(CustomerAnswerDto dto){
    	AjaxRes ar=getAjaxRes();
    	
    	/*
    	 *1.判断权限 
    	 *2.调用新增接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:customerAnswer:add"))){
        	try {
        		User user=ShiroUtil.getCurrentUser();
        		dto.setDelFlag("0");
				Response<CustomerAnswerDto> response=customerAnswerServiceFacade.save(dto);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.SAVE_SUCCEED);
				}else{
					ar.setFailMsg(Const.SAVE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.SAVE_FAIL);
			}
    	}
    	return ar;
    }
	
	@RequestMapping(value="toView")
	public String toView(Long projectQuestionnaireId, Long customerId, String customerPhone, ModelMap map){
		//if(doSecurityIntercept(Const.RESOURCES_TYPE_MENU)){
			//查询菜单按钮权限
			map.put("permitBtn", getPermitBtn(Const.RESOURCES_TYPE_FUNCTION));	
			
			map.addAttribute("customerId", customerId);
			map.addAttribute("customerPhone", customerPhone);
			//查询模板名称及模板描述
			Response<ProjectQuestionnaire> response = projectQuestionnaireServiceFacade
					.findById(projectQuestionnaireId);
			if(response.isSuccess() && response.getEntity() != null){
				map.addAttribute("name", response.getEntity().getName());
				map.addAttribute("projectId", response.getEntity().getProjectId());
				map.addAttribute("projectQuestionnaireId", projectQuestionnaireId);
			}
			
			return "/customer/questionnaire/historicalRecord_view";
		//}
		//return Const.NO_AUTHORIZED_URL;
	}
	
	//查看
	@RequestMapping(value="view", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  view(Long id){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用查看接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:customerAnswer:view"))){
        	try {
				Response<CustomerAnswer> response=customerAnswerServiceFacade.findById(id);
				if(response.isSuccess()){
					ar.setSucceed(response.getEntity());
				}else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
    	}
    	return ar;
    }
	
	//修改
	@RequestMapping(value="edit", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  edit(CustomerAnswerDto dto){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用修改接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:customerAnswer:edit"))){
        	try {
        		Response<Integer> response=customerAnswerServiceFacade.updateByPrimaryKey(dto, true);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.UPDATE_SUCCEED);
				}else{
					ar.setFailMsg(Const.UPDATE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.UPDATE_FAIL);
			}
    	}
    	return ar;
    }
	
	//删除
	@RequestMapping(value="delete", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  delete(Long id){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用查看接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:customerAnswer:delete"))){
        	try {
        		Response<Integer> response=customerAnswerServiceFacade.deleteByPrimaryKey(id);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.DEL_SUCCEED);
				}else {
					ar.setFailMsg(Const.DEL_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DEL_FAIL);
			}
    	}
    	return ar;
    }
	
	//分页查询
	@RequestMapping(value="list", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  list(CustomerAnswerDto dto,Integer pageSize,Integer pageNum){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用分页查询接口
    	 *3.返回信息
    	*/
        //if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:customerAnswer:list"))){
        	try {
        		Page<CustomerAnswer> response=customerAnswerServiceFacade.findPageByDto(dto, PageUtil.begin(pageNum, pageSize),pageSize);
                if(response.getSuccess()){
                    if(null==response.getRows())response.setRows(new ArrayList<CustomerAnswer>());
                	response.setPageNumber(pageNum);
                	ar.setSucceed(response);
                }else{
                	ar.setFailMsg(Const.DATA_FAIL);
                }
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
    	//}
    	return ar;
    }
	
	//保存问卷答案
	@RequestMapping(value="addOrUpdate", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes addOrUpdate(HttpServletRequest request){
    	AjaxRes ar=getAjaxRes();
    	
    	/*
    	 *1.判断权限 
    	 *2.调用新增接口
    	 *3.返回信息
    	*/
        //if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:questionnaireQuestion:add"))){
        	try {
        		//判断是否编辑
        		String edit = request.getParameter("edit");
        		//客户ID
        		String customerId = request.getParameter("customerId");
        		if(StringUtils.isBlank(customerId)){
        			ar.setFailMsg(Const.SAVE_FAIL);
        			return ar;
        		}
        		//项目问卷表ID
        		String projectQuestionnaireId = request.getParameter("questionnaireId");
        		if(StringUtils.isBlank(projectQuestionnaireId)){
        			ar.setFailMsg(Const.SAVE_FAIL);
        			return ar;
        		}
        		//获取答题内容
        		String arrayJson = request.getParameter("json");
        		if(StringUtils.isBlank(arrayJson)){
        			ar.setFailMsg(Const.SAVE_FAIL);
        			return ar;
        		}

        		User user=ShiroUtil.getCurrentUser();
        		CustomerAnswerInfo dto = new CustomerAnswerInfo();
        		dto.setCustomerId(Long.parseLong(customerId));
        		//dto.setProjectQuestionnaireId(Long.parseLong(projectQuestionnaireId));
        		dto.setArrayJson(arrayJson); 
    			dto.setCreateUser(user.getId());
    			dto.setCreateTime(new Date());
    			Response<CustomerAnswer> response = new Response<CustomerAnswer>();
    			if("e".equals(edit)){
    			    dto.setProjectQuestionnaireId(Long.parseLong(projectQuestionnaireId));
    				response = customerAnswerServiceFacade.edit(dto);
    			}else{
    			    dto.setProjectQuestionnaireId(Long.parseLong(projectQuestionnaireId));
    				response = customerAnswerServiceFacade.add(dto);
    			}
    			
    			if(response.isSuccess()){
    				ar.setSucceed(response.getEntity());
				}else{
					ar.setFailMsg(Const.SAVE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.SAVE_FAIL);
			}
    	//}
    	return ar;
    }
	
	/**
	 * 获取客户问卷填写记录的答案
	 * @param request
	 * @return
	 */
	@RequestMapping(value="getAnswer", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  getAnswer(HttpServletRequest request){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用查看接口
    	 *3.返回信息
    	*/
        //if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:customerAnswer:view"))){
        	try {
        		String projectQuestionnaireId = request.getParameter("projectQuestionnaireId");
        		String customerId = request.getParameter("customerId");
        		
        		CustomerAnswerDto dto = new CustomerAnswerDto(); 
        		dto.setProjectQuestionnaireId(Long.parseLong(projectQuestionnaireId));
        		dto.setCustomerId(Long.parseLong(customerId));
        		dto.setDelFlag(DelFlagEnum.UNDEL.getNo());
        		ResponseList<CustomerAnswer> response=customerAnswerServiceFacade.findByDto(dto);
				if(response.isSuccess()){
					if(null==response.getEntity())response.setEntity(new ArrayList<CustomerAnswer>());
                	ar.setSucceed(response);
				}else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
    	//}
    	return ar;
    }
	
	/**
	 * 获取客户列表,客户的问题答案
	 * @param request
	 * @return
	 */
	@RequestMapping(value="getCustomerAnswer", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  getCustomerAnswer(HttpServletRequest request){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用查看接口
    	 *3.返回信息
    	*/
        //if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:customerAnswer:view"))){
        	try {
        		String projectQuestionnaireId = request.getParameter("projectQuestionnaireId");
        		String customerId = request.getParameter("customerId");
        		
        		CustomerAnswerDto dto = new CustomerAnswerDto(); 
        		dto.setProjectQuestionnaireId(Long.parseLong(projectQuestionnaireId));
        		dto.setCustomerId(Long.parseLong(customerId));
        		ResponseList<CustomerAnswer> response=customerAnswerServiceFacade.selectListInfoByDto(dto);
        		if(response.isSuccess()){
					if(null==response.getEntity())response.setEntity(new ArrayList<CustomerAnswer>());
                	ar.setSucceed(response);
				}else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
    	//}
    	return ar;
    }

}
